package com.micrometa.km.ui.components.toolbar;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.pushingpixels.substance.api.SubstanceLookAndFeel;

import test.check.ScrollablePanel;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import com.micrometa.km.ui.skins.Constants;
import com.micrometa.km.util.PropertyConstants;
import com.micrometa.km.util.PropertyUtil;

public class ConfigDialog extends JDialog {
	private static final long serialVersionUID = 1L;

	public ConfigDialog() {
		this.setTitle("设置");
		// this.setAlwaysOnTop(true);
		//this.setSize(200, 200);
		this.setResizable(true);
		this.setLayout(new FlowLayout());
		FormLayout lm = new FormLayout(
				"right:pref, 4dlu, left:pref:grow", "");
		DefaultFormBuilder builder = new DefaultFormBuilder(lm,
				new ScrollablePanel());
		builder.setDefaultDialogBorder();
		this.add(builder.getPanel());

		final JTextField rootl = new JTextField();
		rootl.setColumns(20);
		rootl.setText(PropertyUtil.repositoryRoot());
		rootl.setEditable(false);
		JButton rootb = new JButton("选择目录");
		rootb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser("./");
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				jfc.showOpenDialog(new JFrame());
				File f = jfc.getSelectedFile();
				if (f != null) {
					rootl.setText(f.getPath());
					// TODO set root path, need relative path
					//PropertyUtil.repositoryRootConfig(f.getPath());
				}
			}
		});
		rootb.setEnabled(false);
		builder.appendSeparator("知识库根目录");
		builder.append(rootl);
		builder.append(rootb);
		//copy or cut
		final JCheckBox action=new JCheckBox("点选为复制，取消为剪切");
		if(PropertyUtil.action().equals(PropertyConstants.ACTION_COPY))
			action.setSelected(true);
		else
			action.setSelected(false);
		action.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(action.isSelected()){
					PropertyUtil.actionConfig(PropertyConstants.ACTION_COPY);
				}else{
					PropertyUtil.actionConfig(PropertyConstants.ACTION_CUT);
				}
			}
		});
		builder.appendSeparator("文件拖入方式设置");
		builder.append(action);
		//set skin
		final JComboBox comboSkin = new JComboBox();
		comboSkin.addItem("    黑色  ");//IconUtil.getIcon("/colors/black")
		comboSkin.addItem("    黑灰  ");
		comboSkin.addItem("    褐色  ");
		comboSkin.addItem("    灰色  ");
		comboSkin.addItem("    银色  ");
		comboSkin.addItem("    蓝色  ");
		comboSkin.addItem("    绿色  ");
		comboSkin.addItem("    墨绿  ");
		comboSkin.addItem("    橙色  ");
		String skinString=PropertyUtil.skin();
		int skinIndex=(skinString!=null&&!"".equals(skinString))?Integer.parseInt(skinString):0;
		comboSkin.setSelectedIndex(skinIndex);
		comboSkin.setAlignmentX (Component.LEFT_ALIGNMENT);
		comboSkin.setAlignmentY (Component.CENTER_ALIGNMENT);
		comboSkin.setMaximumRowCount(comboSkin.getItemCount());
		comboSkin.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(comboSkin.getSelectedIndex()==0){
					SubstanceLookAndFeel.setSkin(Constants.geminiSkin);
					PropertyUtil.skinConfig("0");
				}else if(comboSkin.getSelectedIndex()==1){
					SubstanceLookAndFeel.setSkin(Constants.twilightSkin);
					PropertyUtil.skinConfig("1");
				}else if(comboSkin.getSelectedIndex()==2){
					SubstanceLookAndFeel.setSkin(Constants.graphiteGlassSkin);
					PropertyUtil.skinConfig("2");
				}else if(comboSkin.getSelectedIndex()==3){
					SubstanceLookAndFeel.setSkin(Constants.cremeCoffeeSkin);
					PropertyUtil.skinConfig("3");
				}else if(comboSkin.getSelectedIndex()==4){
					SubstanceLookAndFeel.setSkin(Constants.mistSilverSkin);
					PropertyUtil.skinConfig("4");
				}else if(comboSkin.getSelectedIndex()==5){
					SubstanceLookAndFeel.setSkin(Constants.customSkin);
					PropertyUtil.skinConfig("5");
				}else if(comboSkin.getSelectedIndex()==6){
					SubstanceLookAndFeel.setSkin(Constants.saharaSkin);
					PropertyUtil.skinConfig("6");
				}else if(comboSkin.getSelectedIndex()==7){
					SubstanceLookAndFeel.setSkin(Constants.emeraldDuskSkin);
					PropertyUtil.skinConfig("7");
				}else if(comboSkin.getSelectedIndex()==8){
					SubstanceLookAndFeel.setSkin(Constants.nebulaBrickWallSkin);
					PropertyUtil.skinConfig("8");
				}
			}
		});
		builder.nextLine();
		builder.appendSeparator("皮肤设置");
		builder.append(comboSkin);
		//
		this.setModal(false);
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(false);
	}
}
